package com.zeng.sim.controller;

import com.zeng.sim.controller.domain.response.Result;
import com.zeng.sim.controller.vo.SysUserVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 描述信息
 *
 * @author zengwanlin
 * @date 2023-12-19T18:00
 */
@RestController
@RequestMapping("/sys")
public class LoginController {

    @PostMapping("/login")
    @ResponseBody
    public Result login(String username, String password, HttpSession session) {
        SysUserVo sysUserVo = new SysUserVo();
        sysUserVo.setUsername(username);
        sysUserVo
            .setAvatar("https://portrait.gitee.com/uploads/avatars/user/1611/4835367_Jmysy_1578975358.png!avatar200");
        session.setAttribute("currentUser", sysUserVo);
        return Result.success("登录成功了");
    }

    @PostMapping("/logout")
    @ResponseBody
    public Result logout(HttpServletResponse response, HttpSession session) {
        // 销毁Session
        session.invalidate();
        // 销毁Cookie
        Cookie cookie = new Cookie("JSESSIONID", "");
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);
        return Result.success("登出成功了");
    }

}
